package org.genntii.warehousesystem.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.genntii.warehousesystem.domain.entity.Employee;
import org.genntii.warehousesystem.mapper.EmployeeMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class DBEmployeeManager implements UserDetailsService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Employee employee = employeeMapper.selectOne(queryWrapper);

        if (employee == null){
            throw new UsernameNotFoundException(username);
        }else {
            return new EmployeeSecurity(employee);
        }
    }
}
